/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package de.hsmannheim.pepa.patientenportal.apanwep.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import de.hsmannheim.pepa.patientenportal.apanwep.service.ClpSerializer;
import de.hsmannheim.pepa.patientenportal.apanwep.service.messageLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Cedric Nana
 */
public class messageClp extends BaseModelImpl<message> implements message {
	public messageClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return message.class;
	}

	@Override
	public String getModelClassName() {
		return message.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _messageId;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setMessageId(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _messageId;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("messageId", getMessageId());
		attributes.put("eventactioncode", getEventactioncode());
		attributes.put("eventdatetime", getEventdatetime());
		attributes.put("eventoutcome", getEventoutcome());
		attributes.put("sourceaddress", getSourceaddress());
		attributes.put("eventid_id", getEventid_id());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long messageId = (Long)attributes.get("messageId");

		if (messageId != null) {
			setMessageId(messageId);
		}

		String eventactioncode = (String)attributes.get("eventactioncode");

		if (eventactioncode != null) {
			setEventactioncode(eventactioncode);
		}

		String eventdatetime = (String)attributes.get("eventdatetime");

		if (eventdatetime != null) {
			setEventdatetime(eventdatetime);
		}

		Long eventoutcome = (Long)attributes.get("eventoutcome");

		if (eventoutcome != null) {
			setEventoutcome(eventoutcome);
		}

		String sourceaddress = (String)attributes.get("sourceaddress");

		if (sourceaddress != null) {
			setSourceaddress(sourceaddress);
		}

		Long eventid_id = (Long)attributes.get("eventid_id");

		if (eventid_id != null) {
			setEventid_id(eventid_id);
		}
	}

	@Override
	public long getMessageId() {
		return _messageId;
	}

	@Override
	public void setMessageId(long messageId) {
		_messageId = messageId;

		if (_messageRemoteModel != null) {
			try {
				Class<?> clazz = _messageRemoteModel.getClass();

				Method method = clazz.getMethod("setMessageId", long.class);

				method.invoke(_messageRemoteModel, messageId);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getEventactioncode() {
		return _eventactioncode;
	}

	@Override
	public void setEventactioncode(String eventactioncode) {
		_eventactioncode = eventactioncode;

		if (_messageRemoteModel != null) {
			try {
				Class<?> clazz = _messageRemoteModel.getClass();

				Method method = clazz.getMethod("setEventactioncode",
						String.class);

				method.invoke(_messageRemoteModel, eventactioncode);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getEventdatetime() {
		return _eventdatetime;
	}

	@Override
	public void setEventdatetime(String eventdatetime) {
		_eventdatetime = eventdatetime;

		if (_messageRemoteModel != null) {
			try {
				Class<?> clazz = _messageRemoteModel.getClass();

				Method method = clazz.getMethod("setEventdatetime", String.class);

				method.invoke(_messageRemoteModel, eventdatetime);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getEventoutcome() {
		return _eventoutcome;
	}

	@Override
	public void setEventoutcome(long eventoutcome) {
		_eventoutcome = eventoutcome;

		if (_messageRemoteModel != null) {
			try {
				Class<?> clazz = _messageRemoteModel.getClass();

				Method method = clazz.getMethod("setEventoutcome", long.class);

				method.invoke(_messageRemoteModel, eventoutcome);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getSourceaddress() {
		return _sourceaddress;
	}

	@Override
	public void setSourceaddress(String sourceaddress) {
		_sourceaddress = sourceaddress;

		if (_messageRemoteModel != null) {
			try {
				Class<?> clazz = _messageRemoteModel.getClass();

				Method method = clazz.getMethod("setSourceaddress", String.class);

				method.invoke(_messageRemoteModel, sourceaddress);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public long getEventid_id() {
		return _eventid_id;
	}

	@Override
	public void setEventid_id(long eventid_id) {
		_eventid_id = eventid_id;

		if (_messageRemoteModel != null) {
			try {
				Class<?> clazz = _messageRemoteModel.getClass();

				Method method = clazz.getMethod("setEventid_id", long.class);

				method.invoke(_messageRemoteModel, eventid_id);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getmessageRemoteModel() {
		return _messageRemoteModel;
	}

	public void setmessageRemoteModel(BaseModel<?> messageRemoteModel) {
		_messageRemoteModel = messageRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _messageRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_messageRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			messageLocalServiceUtil.addmessage(this);
		}
		else {
			messageLocalServiceUtil.updatemessage(this);
		}
	}

	@Override
	public message toEscapedModel() {
		return (message)ProxyUtil.newProxyInstance(message.class.getClassLoader(),
			new Class[] { message.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		messageClp clone = new messageClp();

		clone.setMessageId(getMessageId());
		clone.setEventactioncode(getEventactioncode());
		clone.setEventdatetime(getEventdatetime());
		clone.setEventoutcome(getEventoutcome());
		clone.setSourceaddress(getSourceaddress());
		clone.setEventid_id(getEventid_id());

		return clone;
	}

	@Override
	public int compareTo(message message) {
		long primaryKey = message.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof messageClp)) {
			return false;
		}

		messageClp message = (messageClp)obj;

		long primaryKey = message.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(13);

		sb.append("{messageId=");
		sb.append(getMessageId());
		sb.append(", eventactioncode=");
		sb.append(getEventactioncode());
		sb.append(", eventdatetime=");
		sb.append(getEventdatetime());
		sb.append(", eventoutcome=");
		sb.append(getEventoutcome());
		sb.append(", sourceaddress=");
		sb.append(getSourceaddress());
		sb.append(", eventid_id=");
		sb.append(getEventid_id());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(22);

		sb.append("<model><model-name>");
		sb.append("de.hsmannheim.pepa.patientenportal.apanwep.model.message");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>messageId</column-name><column-value><![CDATA[");
		sb.append(getMessageId());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>eventactioncode</column-name><column-value><![CDATA[");
		sb.append(getEventactioncode());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>eventdatetime</column-name><column-value><![CDATA[");
		sb.append(getEventdatetime());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>eventoutcome</column-name><column-value><![CDATA[");
		sb.append(getEventoutcome());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>sourceaddress</column-name><column-value><![CDATA[");
		sb.append(getSourceaddress());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>eventid_id</column-name><column-value><![CDATA[");
		sb.append(getEventid_id());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _messageId;
	private String _eventactioncode;
	private String _eventdatetime;
	private long _eventoutcome;
	private String _sourceaddress;
	private long _eventid_id;
	private BaseModel<?> _messageRemoteModel;
}